package com.yh.homestay.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.common.OwnerEditResult;
import com.yh.homestay.dao.UserOwnerLikeMapper;
import com.yh.homestay.dao.UserOwnerMapper;
import com.yh.homestay.pojo.UserOwner;
import com.yh.homestay.pojo.UserOwnerExample;
import com.yh.homestay.service.UserOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserOwnerServiceImpl implements UserOwnerService {

    @Autowired
    private UserOwnerMapper userOwnerMapper;
    @Autowired
    private UserOwnerLikeMapper userOwnerLikeMapper;

    @Override
    public HomeStayResult selectAll(String ownerName, Integer page, Integer limit) {
        Map map=new HashMap();
        map.put("ownerName",ownerName);
        /*UserOwnerExample userOwnerExample = new UserOwnerExample();
        UserOwnerExample.Criteria criteria = userOwnerExample.createCriteria();*/
        PageHelper.startPage(page,limit);
        List<UserOwner> owner = userOwnerLikeMapper.findByName(map);
        PageInfo pageInfo = new PageInfo(owner);
        HomeStayResult res=new HomeStayResult(0," ", (int) pageInfo.getTotal(),pageInfo.getList());
        return res;
    }

    @Override
    public HomeStayResult addOwner(UserOwner userOwner) {
        int insert = userOwnerMapper.insert(userOwner);
        if (insert > 0) {
            return new HomeStayResult(200, "ok",0,null);
        } else {
            return new HomeStayResult(500, "error",0,null);
        }
    }

    @Override
    public HomeStayResult delete(Integer id) {
        UserOwner userOwner = new UserOwner();
        userOwner.setOwnerId(id);
        int delete = userOwnerMapper.delete(userOwner);
        if (delete > 0) {
            return new HomeStayResult(200, "ok",0,null);
        } else {
            return new HomeStayResult(500, "error",0,null);
        }
    }

    @Override
    public HomeStayResult update(Integer id, String ownerName, String ownerPhone, String ownerPhoto, String ownerIdcardA, String ownerIdcardB, String ownerRoomIds) {
        UserOwner userOwner = new UserOwner();
        //   userOwner.setOwnerId(id);
        userOwner.setOwnerName(ownerName);
        userOwner.setOwnerPhone(ownerPhone);
        userOwner.setOwnerPhoto(ownerPhoto);
        userOwner.setOwnerIdcardA(ownerIdcardA);
        userOwner.setOwnerIdcardB(ownerIdcardB);
        userOwner.setOwnerRoomIds(ownerRoomIds);
        UserOwnerExample example = new UserOwnerExample();
        UserOwnerExample.Criteria criteria = example.createCriteria();
        criteria.andOwnerIdEqualTo(id);
        int update = userOwnerMapper.updateByExampleSelective(userOwner, example);
        if (update > 0) {
            return new HomeStayResult(200, "ok",0,null);
        } else {
            return new HomeStayResult(500, "error",0,null);
        }
    }

    @Override
    public OwnerEditResult selectOne(Integer id) {
        UserOwnerExample userOwnerExample = new UserOwnerExample();
        UserOwnerExample.Criteria criteria = userOwnerExample.createCriteria();
        criteria.andOwnerIdEqualTo(id);
        List<UserOwner> userOwners = userOwnerMapper.selectByExample(userOwnerExample);
        if (userOwners != null && userOwners.size() > 0) {
            return new OwnerEditResult(200, "ok", userOwners.get(0));
        } else {
            return new OwnerEditResult(500, "err", null);
        }
    }

//    @Override
//    public ResultMessage selectByName(String ownerName) {
//        List<UserOwner> byName = userOwnerMapper.findByName(ownerName);
//        return new ResultMessage(200, "ok", byName.size(), byName);
//
//    }

    @Override
    public HomeStayResult delectAll(List<Integer> ids) {
        UserOwnerExample example = new UserOwnerExample();
        UserOwnerExample.Criteria criteria = example.createCriteria();
        criteria.andOwnerIdIn(ids);
        int delete = userOwnerMapper.deleteByExample(example);
        if (delete > 0) {
            return new HomeStayResult(200, "ok",0,null);
        } else {
            return new HomeStayResult(500, "error",0,null);
        }
    }


}
